<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\News as NewsModel;
use app\common\model\ZhNews as ZhNewsModel;
use app\common\model\NewsLike as NewsLikeModel;
use app\common\model\Company as CompanyModel;
use app\common\model\Cate as CateModel;
use app\common\model\NewsComment as NewsCommentModel;
use app\common\model\VideoNew as VideoNewModel;
use app\common\model\Music as MusicModel;
use app\common\model\Ilike as IlikeModel;
use app\common\model\ZhSupport as ZhSupportModel;
use app\common\model\Member as MemberModel;
use app\common\model\ZhSupportEducation as ZhSupportEducationModel;
use app\common\model\ZhSupportDoubt as ZhSupportDoubtModel;
use app\common\model\ZhSupportReply as ZhSupportReplyModel;

/**
 * News资讯控制器
 * Class Banner
 * @package app\home\controller
 */
class News extends Base
{
    /**
     * [getnewsbycomid  获取寺院资讯]
     * @param
     * @return json
     * @author [C。]
     */
    public function getnewsbycomid()
    {
        $param = input('param.');
        $model = new ZhNewsModel();
        $this->getPageAndSize($param);
        try{
            $map['states'] = 1;
            $map['cid'] = 1;
            $map['comid'] = $param['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,title,create_time,jread_num,read_num,img');
            foreach($lists as $k=>$v){
                $lists[$k]['cover'] = $lists[$k]['img'];
                $lists[$k]['type'] = 1;
//                $lists[$k]['content'] = percent($v['content']);
                $lists[$k]['read_num'] = $v['read_num'] + $v['jread_num'];
                $lists[$k]['addtime'] = date('Y-m-d',strtotime($v['create_time']));
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  获取每日一禅]
     * @param
     * @return json
     * @author [C。]
     */
    public function getDaily()
    {
        $model = new NewsModel();
        $map = [];
        try{
            $map['cateid'] = 7;
            $map['states'] = 1;
            $lists = $model->getOne($map,'id,cover,content,share_top,share_buttom,from');
            $lists['content'] = percent($lists['content']);
            $http = substr($lists['cover'],0,4);
            if($http != 'http') {
                $lists['cover'] = config('domain_name') . $lists['cover'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$lists);
    }
    /**
     * [getNews  根据分类id获取相应资讯]
     * @param
     * @return json
     * @author [C。]
     */
    public function getDailymore()
    {
        $model = new NewsModel();
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['states'] = 1;
            $map['cateid'] = 7;
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlists($map,$this->from, $this->size,'id,cover,content');
            foreach($lists as $k=>$v){
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
                $lists[$k]['content'] = percent($v['content']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getNews  根据分类id获取相应资讯]
     * @param
     * @return json
     * @author [C。]
     */
    public function getNews()
    {
        $model = new NewsModel();
        $cmodel = new CompanyModel();
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['states'] = 1;
            $map['cateid'] = $param['cateid'];
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / 10));//总页数
            $lists = $model->getlists($map,$this->from, 10);
            foreach($lists as $k=>$v){
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
                $lists[$k]['content'] = percent($v['content']);
                $lists[$k]['addtime'] = date('Y-m-d',$lists[$k]['addtime']);
                $lists[$k]['read_num'] = $v['read_num'];
//                if(!empty($v['comid'])){
//                    $company = $cmodel->findData($v['comid'],'id','comname');
//                    $lists[$k]['comname'] = $company['comname'];
//                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getSearch  首页搜索]
     * @param type 1资讯 2视频 3音乐 4寺院
     * @return json
     * @author [C。]
     */
    public function getSearch()
    {
        $param = input('param.');
        if(empty($param['key'])){
            return admin_json(0, '搜索内容为空');
        }

        $this->getPageAndSize($param);
        $user = $this->user;
        $map = [];
        try{
            if(!empty($param['type'])) {
                if ($param['type'] == 1) {
                    $model = new NewsModel();
                    $camodel = new CateModel;
                    $map2['class_id'] = 1;
                    $map2['states'] = 1;
                    $cate = $camodel->getConditionByFields($map2, 'id,cname');
                    $ida = array_column($cate, 'id');
                    $ids = join(',', $ida);

                    $map['cateid'] = ['in', $ids];
                    $map['states'] = 1;
                    $map['title'] = ['like', "%" . $param['key'] . "%"];
                    $count = $model->getCounts($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlists($map, $this->from, $this->size, 'id,title,cover,addtime,read_num,jread_num');

                    foreach ($lists as $v) {
                        $http = substr($v['cover'], 0, 4);
                        if ($http != 'http') {
                            $v['cover'] = config('domain_name') . $v['cover'];
                        }
                        $v['addtime'] = get_last_time($v['addtime']);
                    }
                } elseif ($param['type'] == 2) {
                    $model = new VideoNewModel;
                    $cmodel = new NewsCommentModel;
                    $lmodel = new NewsLikeModel;
                    $map['states'] = 1;
                    $map['title'] = ['like', "%" . $param['key'] . "%"];
                    $count = $model->getCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlist($map, $this->from, $this->size, 'id,title,cover,share_num,label,create_time');
                    foreach ($lists as &$v) {
                        $v['label'] = explode(',', $v['label']);
                        $comment_num = $cmodel->getAllCount(['nid' => $v['id'], 'mtype' => 2]);
                        $v['comment_num'] = $comment_num;
                        $v['like_num'] = $lmodel->getAllCount(['nid' => $v['id'], 'mtype' => 2]);
                    }
                } elseif ($param['type'] == 3) {
                    $model = new MusicModel;
                    $map['n.states'] = 1;
                    $map['n.title'] = ['like', "%" . $param['key'] . "%"];
                    $count = $model->getCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getlist($map, $this->from, $this->size, 'n.id,n.title,n.img,n.url,n.author');
                    foreach ($lists as &$v) {
                        $http = substr($v['img'], 0, 4);
                        if ($http != 'http') {
                            $v['img'] = config('domain_name') . $v['img'];
                        }
                        $http1 = substr($v['url'], 0, 4);
                        if ($http1 != 'http') {
                            $v['url'] = config('domain_name') . $v['url'];
                        }
                    }
                } elseif ($param['type'] == 4) {
                    $model = new CompanyModel;
                    $cmodel = new IlikeModel();
                    $map['states'] = 1;
                    $map['is_show'] = 1;
                    $map['comname'] = ['like', "%" . $param['key'] . "%"];
                    $count = $model->getCompanyCount($map);//满足条件的总数
                    $allpage = intval(ceil($count / $this->size));//总页数
                    $lists = $model->getCompanyByConditons($map, $this->from, $this->size, 'id,comname,compic,focus,province,city', 2);
                    foreach ($lists as $k => $v) {
                        $focus = $cmodel->getAllCount(['status' => 1, 'itemid' => $v['id']]);
                        $lists[$k]['focusnum'] = $v['focus'] + $focus;
                        $map1 = array(
                            'itemid' => $v['id'],
                            'memid' => $user['id']
                        );
                        $focus = $cmodel->findDataByComid($map1);
                        if (!empty($focus) && $focus['status'] == 1) {
                            $lists[$k]['focus'] = 1;
                        } else {
                            $lists[$k]['focus'] = 0;
                        }
                        $http = substr($v['compic'], 0, 4);
                        if ($http != 'http') {
                            $lists[$k]['compic'] = config('domain_name') . $v['compic'];
                        }
                    }
                }
            }else{
                $model = new NewsModel();
                $cmodel = new CompanyModel();
                $camodel = new CateModel;
                $map2['class_id'] = 1;
                $map2['states'] = 1;
                $cate = $camodel->getConditionByFields($map2,'id,cname');
                $ida = array_column($cate,'id');
                $ids = join(',',$ida);
                $map['cateid'] = ['in',$ids];
                $map['states'] = 1;
                $map['title'] = ['like', "%" . $param['key'] . "%"];
                $news = $model->getlists($map,0,10);
                $map1['comname'] = ['like', "%" . $param['key'] . "%"];
                $map1['states'] = 1;
                $temple = $cmodel->getCompanyByConditons($map1,0,10,'id,comname,compic,addtime,read_num');
                foreach($temple as $k1=>$v1){
                    $thttp = substr($temple[$k1]['compic'],0,4);
                    if($thttp != 'http'){
                        $temple[$k1]['compic'] = config('domain_name').$temple[$k1]['compic'];
                    }
                    $temple[$k1]['addtime'] = date('Y-m-d',$temple[$k1]['addtime']);
                    if(empty($temple[$k1]['read_num'])){
                        $temple[$k1]['read_num'] = 0;
                    }
                }
                foreach($news as $k=>$v){
                    $http = substr($v['cover'],0,4);
                    if($http != 'http'){
                        $news[$k]['cover'] = config('domain_name').$v['cover'];
                    }
                    $news[$k]['addtime'] = date('Y-m-d',$news[$k]['addtime']);
                }
                $data = array(
                    'temple' => $temple,
                    'news' => $news,
                );
                return admin_json(1, '获取成功',$data);
            }
            add_quality($user['id'],30,$user['quality'],4);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getNew  获取一条和资讯内容]
     * @param
     * @return json
     * @author [C。]
     */
    public function getNew()
    {
        $model = new NewsModel();
        $lmodel = new NewsLikeModel;
        $param = input('param.');
        $user = $this->user;
        $map = [];
        try{
            $map['id'] = $param['id'];
            $lists = $model->getOne($map,'id,title,content,read_num,cover,comid,addtime,from,jread_num');
            $http = substr($lists['cover'],0,4);
            if($http != 'http'){
                $lists['cover'] = config('domain_name').$lists['cover'];
            }
            $lists['content'] = percent($lists['content']);
            $num = $lists['read_num'] + 1;
            $model->editData(['id'=>$lists['id'],'read_num'=>$num]);
            $lists['read_num'] = $lists['read_num'] + $lists['jread_num'];
            $lists['content'] = get_img_thumb_url($lists['content'],config('domain_name').'/');
            $lists['addtime'] = date('Y-m-d H:i:s',$lists['addtime']);
            if(empty($lists['from'])){
                $lists['from'] = '无';
            }
            $likes = $lmodel->getAllCount(['nid'=>$param['id'],'status'=>1]);
            if(!empty($user)){
                $is_like = $lmodel->findOne(['nid'=>$param['id'],'mid'=>$user['id'],'status'=>1]);
                if(empty($is_like)){
                    $lists['is_like'] = 0;
                }else{
                    $lists['is_like'] = 1;
                }
                $lists['likes'] = $likes;
            }
            if(!empty($user['id'])){
                add_quality($user['id'],28,$user['quality'],4);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$lists);
    }
    /**
     * [getRecommend  获取推荐文章]
     * @param
     * @return json
     * @author [C。]
     */
    public function getRecommend()
    {
        $model = new NewsModel();
        $param = input('param.');
        $this->getPageAndSize($param);
        $map = [];
        try{
            $map['states'] = 1;
            $map['recommend1'] = 1;
            $count = $model->getCounts($map);//满足条件的总数
            $allpage = intval(ceil($count / 4));//总页数
            $lists = $model->getlists($map,$this->from, 4);
            foreach($lists as $k=>$v){
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $lists[$k]['cover'] = config('domain_name').$v['cover'];
                }
                $lists[$k]['addtime'] = date('Y-m-d',$lists[$k]['addtime']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getAllnews  获取所有文章]
     * @param
     * @return json
     * @author [C。]
     */
    public function getAllnews()
    {
        $model = new NewsModel;
        $cmodel = new CateModel;
        $map = [];
        try{
            $cate = $cmodel->getAllData(['class_id'=>1,'states'=>1],'sort','id,cname','asc');
            $map['states'] = 1;
            foreach($cate as &$v){
                $map['cateid'] = $v['id'];
                $news = $model->getlists($map,0,4,'id,title,cover,comid,addtime,read_num,istop,recommend,from,jread_num');
                foreach($news as $v1) {
                    $http = substr($v1['cover'], 0, 4);
                    if ($http != 'http') {
                        $v1['cover'] = config('domain_name') . $v1['cover'];
                    }
                    $v1['addtime'] = date('Y-m-d', $v1['addtime']);
                    $v1['read_num'] = $v1['read_num'] + $v1['jread_num'];
                }
                $v['new'] = $news;
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',$cate);
    }
}